<?php

namespace app\common\model;

use think\Model;

class Base extends Model
{


    /**
     * 获取所有列表信息
     * @param array $condition
     * @param array $fields
     * @param array $order
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public static function getAll($condition = [], $fields = [],$order = []){
        $all = self::where($condition)
            ->when($fields, function ($query) use ($fields) {
                return $query->field($fields);
            })
            ->when($order, function ($query) use ($order) {
                return $query->order($order[0],isset($order[1]) ? $order[1] : null);
            })
            ->select();
        return $all ? $all->toArray() : [];
    }

    /**
     * 获取单个信息
     * @param array $condition
     * @return array|null|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getInfo($condition = [], $fields = [],$order = []){
        return self::where($condition)
            ->when($fields, function ($query) use ($fields) {
                return $query->field($fields);
            })
            ->when($order, function ($query) use ($order) {
                return $query->order($order[0],isset($order[1]) ? $order[1] : null);
            })
            ->find();
    }

    /**
     * 获取列表信息，分页
     * @param array $condition
     * @param array $fields
     * @param array $order
     * @param int $per_page
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public static function getList($condition = [], $fields = [],$order = [],$per_page = 10){
        return self::where($condition)
            ->when($fields, function ($query) use ($fields) {
                return $query->field($fields);
            })
            ->when($order, function ($query) use ($order) {
                return $query->order($order[0],isset($order[1]) ? $order[1] : null);
            })
            ->paginate($per_page);

    }

    /**
     * 统计数量
     * @param array $condition
     * @return float|string
     */
    public static function getCount($condition= []){
        return self::where($condition)->count();
    }


    /**
     * 更新数据
     * @param $update
     * @param $condition
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function edit($update, $condition)
    {
        return self::where($condition)->update($update);
    }

    /**
     * 删除数据
     * @param $id
     * @return int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function del( $id ) {
        if( is_numeric($id) ){
            $condition = ['id' => $id];
        }else{
            $condition = $id;
        }
        return self::where($condition)->delete();
    }

}
